#include<stdio.h>
 struct node{
	char data;
	int left;
	int right;
}t1[10],t2[10];

int build(struct node t[]){
	int n;
	char ch1,ch2;
	scanf("%d",&n);
	if(n==0)
	return -1;
	bool test[n]={false};
	for(int i=0;i<n;i++){
		getchar();
		scanf("%c %c %c",&t[i].data,&ch1,&ch2);
		if(ch1!='-'){
			t[i].left=ch1-'0';
			test[t[i].left]=true; 
		}
		else{
			t[i].left=-1;
		}
		if(ch2!='-'){
			t[i].right=ch2-'0';
			test[t[i].right]=true;
		}
		else{
			t[i].right=-1;
		}
	}
	int j;
	for(j=0;j<n;j++){
			if(test[j]==false)
			break;
		}
			return j; 
}
int match(int r1,int r2){
	if(r1==-1&&r2==-1)
		return 1;
	if((r1==-1&&r2!=-1)||(r1!=-1&&r2==-1))
		return 0;
	if(t1[r1].data!=t2[r2].data)
		return 0;
	else{
		if(match(t1[r1].left,t2[r2].left)&&match(t1[r1].right,t2[r2].right))
			return 1;
		else if(match(t1[r1].left,t2[r2].right)&&match(t1[r1].right,t2[r2].left))
			return 1;
		else
			return 0;
		}
}
int main(){
	int root1,root2;
	root1=build(t1);
	root2=build(t2);
	if(match(root1,root2)){
		printf("yes");
	}
	else{
		printf("no");
	}
	return 0;
} 
